﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelTap : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public ButtonDown buttonDown;
    private int totalTime = 10;
    private int targetCount = 50;
    private int curCount = 0;
    private bool start = false;
    public Text tipText;
    public Text timeText;
    public Text countText;
    public Text loveText;
    //public TextAlignment 
    // Start is called before the first frame update
    void Start()
    {
        buttonDown.onButtonDown.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            if (!start)
            {
                start = true;
                Invoke("CD", 1);
            }
            curCount++;
            UpdateCountText();
        });
        loveText.text = string.Empty;

        UpdateTimeText();
        UpdateCountText();
        tipText.text = Localization.Instance.GetString("LevelTapTip");

    }

    private void CD()
    {
        if (totalTime > 0)
        {
            totalTime--;
            UpdateTimeText();
            Invoke("CD", 1);
            if (totalTime == 0)
            {
                if (curCount >= targetCount)
                {
                    gameLevelItem.TriggerResult(true);
                    loveText.text = UConst.Str_TargetLove;
                }
                else
                    gameLevelItem.TriggerResult(false);
            }

        }

    }

    private void UpdateTimeText()
    {
        timeText.text = Localization.Instance.GetFormatString("str_time", totalTime);
    }
    private void UpdateCountText()
    {
        countText.text = Localization.Instance.GetFormatString("str_count", curCount);
    }
}
